package wwiii;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;

public class GameManager {
	@SuppressWarnings("unchecked")
	public boolean gameExists(String id) {
		final PersistenceManager pm = PMF.getPM();

		try {
			final Query query = pm.newQuery(Game.class);
			query.setFilter("key == keyParam");
			query.declareParameters(Key.class.getName() +" keyParam");

			final List<Game> games = (List<Game>) query.execute(Game.getKey(id));

			if (games.size() > 1) {
				throw new IllegalArgumentException("GameID is ambigious");
			}

			if (games.isEmpty()) {
				return false;
			} else {
				return true;
			}
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public void clearGames() {
		final PersistenceManager pm = PMF.getPM();

		try {
			final Query query = pm.newQuery(Game.class);
			final List<Game> games = (List<Game>) query.execute();

			pm.deletePersistentAll(games);
		} finally {
			pm.close();
		}
	}

	public void storeGame(Game game) {
		final PersistenceManager pm = PMF.getPM();

		try {
			pm.makePersistent(game);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public void removeGame(String id) {
		if (id == null) {
			throw new IllegalArgumentException("GameID is null");
		}
		if (id.isEmpty()) {
			throw new IllegalArgumentException("GameID is empty");
		}

		final PersistenceManager pm = PMF.getPM();

		try {
			final Query query = pm.newQuery(Game.class);
			query.setFilter("key == keyParam");
			query.declareParameters(Key.class.getName() +" keyParam");

			final List<Game> games = (List<Game>) query.execute(Game.getKey(id));

			if (games.isEmpty()) {
				throw new IllegalArgumentException("GameID '"+ id +"' is unknown");
			}
			if (games.size() > 1) {
				throw new IllegalArgumentException("GameID '"+ id +"' is ambigious");
			}

			pm.deletePersistent(games.get(0));
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public Game loadGame(String id) {
		if (id == null) {
			throw new IllegalArgumentException("GameID is null");
		}
		if (id.isEmpty()) {
			throw new IllegalArgumentException("GameID is empty");
		}

		final PersistenceManager pm = PMF.getPM();

		try {
			final Query query = pm.newQuery(Game.class);
			query.setFilter("key == keyParam");
			query.declareParameters(Key.class.getName() +" keyParam");

			final List<Game> games = (List<Game>) query.execute(Game.getKey(id));

			if (games.isEmpty()) {
				throw new IllegalArgumentException("GameID is unknown");
			}
			if (games.size() > 1) {
				throw new IllegalArgumentException("GameID is ambigious");
			}

			return games.get(0);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public Collection<Game> loadGames() {
		final PersistenceManager pm = PMF.getPM();

		try {
			final Query query = pm.newQuery(Game.class);

			final List<Game> games = (List<Game>) query.execute();

			return new ArrayList<Game>(games);
		} finally {
			pm.close();
		}
	}
}
